package cn.est.controller;
import cn.est.pojo.Classify;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.ClassifyService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/classify")
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Classify> searchClassify(@RequestParam Map<String,Object> param) {
        Page<Classify> classifyPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            classifyPage  = classifyService.queryClassifyPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classifyPage;
    }

    @RequestMapping("/saveOrUpdateClassify")
    @ResponseBody
    public Result saveOrUpdateClassify(Classify classify){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(classify.getId())){
                flag=classifyService.qdtxAddClassify(classify);
            }else{
                flag=classifyService.qdtxModifyClassify(classify);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getClassifyById")
    @ResponseBody
    public Result getClassifyById(Long id){
        Result result= ResultUtils.returnSuccess();
        Classify classify=null;
        try {
            classify=classifyService.getClassifyById(id);
            result.setData(classify);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveClassify")
    @ResponseBody
    public Result batchRemoveClassify(String ids){
        Integer flag=0;
        try {
            flag=classifyService.qdtxBatchDeleteClassify(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeClassifyById")
    @ResponseBody
    public Result removeClassifyById(Long id){
        Integer flag=0;
        try {
            flag=classifyService.qdtxDeleteClassifyById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
